package com.jian.selector;

import org.junit.Test;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.*;
import java.util.Date;
import java.util.Set;

/**
 * @author Jiange
 * @create 2021-09-13-16:09
 */
public class SelectorDemo1 {
    public static void main(String[] args) throws IOException {
        Selector selector = Selector.open();
        //并不是所有的channel都可以注册到selector里面 只有实现了SelectableChannel的类可以
        ServerSocketChannel socketChannel = ServerSocketChannel.open();
        socketChannel.socket().bind(new InetSocketAddress(8080));
        socketChannel.configureBlocking(false);

        //在将channel注册到select的时候  channel必须处于非阻塞的状态，否则报错 FileChannel就不能使用selector
        //它无法设置为非阻塞状态

        //一个通道  并没有一定要支持所有的四种操作
        socketChannel.register(selector, SelectionKey.OP_ACCEPT);//第二参数表示监听状态
        int select = selector.select();//查询出已经就绪的通道  不传参表示阻塞到至少有一个通道就绪了  selectNow表示只要有一个就绪就立即返回 非阻塞
        //返回值表示有多少个通道就绪的数量
        Set<SelectionKey> selectionKeys = selector.selectedKeys();
        for (SelectionKey selectionKey : selectionKeys) {
            if(selectionKey.isAcceptable()){

            }else if(selectionKey.isConnectable()){

            }
        }
    }

    @Test
    public void client() throws IOException {
        SocketChannel socketChannel = SocketChannel.open(new InetSocketAddress("127.0.0.1", 8888));
        socketChannel.configureBlocking(false);

        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        byteBuffer.put(new Date().toString().getBytes());

        byteBuffer.flip();
        socketChannel.write(byteBuffer);
        byteBuffer.clear();
    }

    @Test
    public void server() throws IOException {
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        serverSocketChannel.bind(new InetSocketAddress(8888));
        serverSocketChannel.configureBlocking(false);

        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        Selector selector = Selector.open();
        serverSocketChannel.register(selector,SelectionKey.OP_ACCEPT);

        while (selector.select() > 0){
            Set<SelectionKey> selectionKeys = selector.selectedKeys();
            for (SelectionKey selectionKey : selectionKeys) {
                if(selectionKey.isAcceptable()){
                    SocketChannel socketChannel = serverSocketChannel.accept();
                    socketChannel.configureBlocking(false);
                    socketChannel.register(selector,SelectionKey.OP_READ);

                }else if(selectionKey.isReadable()){
                    SocketChannel channel = (SocketChannel) selectionKey.channel();
                    ByteBuffer buffer = ByteBuffer.allocate(1024);
                    int read = channel.read(buffer);
                    while(read > 0){
                        buffer.flip();
                        System.out.println(new String(buffer.array(),0,read));
                        buffer.clear();
                        read = channel.read(buffer);
                    }
                }
            }
        }
        System.out.println("Jian");
    }
}
